package com.zzb.collection;

import java.util.*;

public class MyMap {
    public static void main(String[] args) {
        Roommate roommate1 = new Roommate("赵丽颖", 18);
        Roommate roommate2 = new Roommate("范冰冰", 20);
        Roommate roommate3 = new Roommate("杨幂", 19);
        Roommate roommate4 = new Roommate("杨幂", 19);

        HashSet<Roommate> hashSet = new HashSet<>();
        hashSet.add(roommate1);
        hashSet.add(roommate2);
        hashSet.add(roommate3);
        hashSet.add(roommate4);

        System.out.println("我的室友是:" + hashSet);
    }

}

class Roommate {
    private String name;
    private int age;

    public Roommate() {
    }

    public Roommate(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Roommate roommate = (Roommate) o;
        return age == roommate.age &&
                name.equals(roommate.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}


class student implements Comparable<student> {
    private String name;
    //学生的姓名
    private int age;


    //学生的年龄
    public student(String name, int age) {
        this.name = name;
        this.age = age;

    }

    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(student o) {
        int result1 = this.age - o.age;                                        // 先比较年龄
        int result2 = result1 == 0 ? this.name.compareTo(o.name) : result1;    // 年龄相同比较姓名
        return result2;
    }
}

class TreeMapDemo01 {
    public static void main(String[] args) {
        TreeMap<student, String> treeMap = new TreeMap<>(); //创建TreeMap集合对象
//添加元素
        treeMap.put(new student("万息", 16), "北京");
        treeMap.put(new student("自习", 16), "西安");
//遍历集合
        treeMap.forEach((student, address) -> System.out.println(student + "----" + address));
    }
}

class Demo02 {

    public static void main(String[] args) {
        HashMap<Integer, String> hashMap = new HashMap<>();
        hashMap.put(101, "阿三面馆");
        hashMap.put(102, "阿四粥馆");
        hashMap.put(103, "阿五米馆");
        hashMap.put(104, "阿六快递");


        //方式一:
        Set<Integer> keySet = hashMap.keySet();
        for (Integer key : keySet) {
            String value = hashMap.get(key);
            System.out.println(key + "---" + value);
        }

        System.out.println("========================");

        //方式二:
        Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            System.out.println(entry.getKey() + "---" + entry.getValue());
        }

        System.out.println("========================");

        //方式三:
        hashMap.forEach(((key, value) -> {
            System.out.println(key + "---" + value);
        }));
    }
}

class Person1 {
    private String name;
    private int age;

    public Person1() {
    }

    public Person1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Test1 {
    public static void main(String[] args) {
        Person1 person0 = new Person1("张三", 18);
        Person1 person1 = new Person1("李四", 20);
        Person1 person2 = new Person1("王五", 35);
        Person1 person3 = new Person1("赵六", 21);
        Person1 person4 = new Person1("田七", 19);

        TreeMap<Person1, Object> map = new TreeMap<>(new Comparator<Person1>() {
            @Override
            public int compare(Person1 o1, Person1 o2) {
                return o2.getAge() - o1.getAge();
            }
        });

        map.put(person0, "北京");
        map.put(person1, "上海");
        map.put(person2, "天津");
        map.put(person3, "北京");
        map.put(person4, "上海");

        //删除、更新数据
        Set<Person1> keySet = map.keySet();
        Iterator<Person1> it = keySet.iterator();
        while (it.hasNext()) {
            Person1 person = it.next();
            if ("张三".equals(person.getName())) {
                it.remove();
            } else if ("李四".equals(person.getName())) {
                map.put(person, "郑州");
            }
        }

        //遍历: 方式二
        Set<Map.Entry<Person1, Object>> entries = map.entrySet();
        for (Map.Entry<Person1, Object> entry : entries) {
            System.out.println(entry.getKey() + " --- " + entry.getValue());
        }
    }
}

